const { http } = require("../../utils/http3000")
Page({

  data: {
    currentCity: "",
    banner: [],
    autoplay: true,
    interval: 3000,
    duration: 1200,
    hot: [],
    music: [],
    anchor:[]
  },
  handleClick() {
    wx.navigateTo({
      url: '/pages/city/city'
    });
  },
  handleClick2() {
    wx.navigateTo({
      url: '/pages/search/search'
    });
  },
  onShow() {
    var city = wx.getStorageSync("city");
    this.setData({
      currentCity: city
    })
  },
  onLoad() {
    /* 1、获取经纬度 */
    wx.getLocation({
      type: 'wgs84',
      altitude: false,
      success: (res) => {
        console.log(res)
        var { latitude, longitude } = res;
        /* 2、根据经纬度获取当前所在的城市 */
        var key = "5SHBZ-N3D3I-M4IGS-5Y7DV-RQXEQ-SRBNV";
        var url = `https://apis.map.qq.com/ws/geocoder/v1/?location=${latitude},${longitude}&key=${key}&get_poi=1`
        wx.request({
          url,
          header: { 'content-type': 'application/json' },
          method: 'GET',
          dataType: 'json',
          responseType: 'text',
          success: (res) => {
            var currentCity = res.data.result.address_component.city;
            var storageCity = wx.getStorageSync("city");
            console.log(storageCity)
            /* 3、当缓存中的城市和当前地理位置所在城市不一致的时候,弹窗 */
            if (currentCity != storageCity) {
              wx.showModal({
                title: '切换城市',
                content: '当时城市不是您所在的城市',
                showCancel: true,
                cancelText: '取消',
                cancelColor: '#000000',
                confirmText: '确定',
                confirmColor: '#3CC51F',
                success: (result) => {
                  if (result.confirm) {
                    /* 确定设置为当前地理位置 */
                    this.setData({
                      currentCity
                    })
                    wx.setStorageSync("city", currentCity);
                  } else {
                    /* 取消就是缓存中的城市 */
                    this.setData({
                      currentCity: storageCity
                    })
                  }
                }
              });
            }

          }
        });
      }
    });
    var url = `http://47.108.197.28:3000/banner`
    http({
      url,
      success: res => {
        var banner = res.data.banners
        this.setData({
          banner
        })
      }
    })
    var top = `http://47.108.197.28:3000/top/playlist/highquality`
    http({
      url: top,
      success: res => {
        var hot= []
        var playlists = res.data.playlists
        playlists.splice(0,3).forEach(item => {
          var { name, coverImgUrl } = item;
          hot.push({
            coverImgUrl,
            name
          })
        })
        this.setData({
          hot
        })
      }
    })
    var album = `http://47.108.197.28:3000/album/newest`
    http({
      url: album,
      success: res => {
        var music=[]
        var albums = res.data.albums
        albums.splice(0,3).forEach(item => {
          var { picUrl, name } = item
          music.push({
            picUrl,
            name
          })
          console.log(music)
        })
        this.setData({
          music
        })
      }
    })
    var person = `http://47.108.197.28:3000/personalized/djprogram`
    http({
      url: person,
      success: res => {
        var anchor = []
        var result = res.data.result
        result.splice(0, 3).forEach(item => {
          var { name, picUrl } = item
          anchor.push({
            picUrl,
            name
          })
        })
        this.setData({
          anchor
        })
      }
    })
  }
})